package com.example.eamh2system.netty.code;

import com.example.eamh2system.netty.message.ModbusMessage;
import com.example.eamh2system.netty.message.ModbusRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ModbusEncoder extends MessageToByteEncoder<ModbusRequest> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ModbusRequest msg, ByteBuf out) throws Exception {
        // 编码 Modbus 请求为字节流
        out.writeByte(0x01); // Modbus 功能码：读取线圈状态
        out.writeShort(msg.getStartAddress()); // 起始地址
        out.writeShort(msg.getQuantity()); // 数量
    }
}
